/*
 *  功能: 暴君之王座 麦格纳斯<简单>
 *  From: 芬芬时尚潮流
 */

//复活次数
var reviveCount = 3;
//GM复活次数
var GMreviveCount = 99;
//开始事件地图
var startMap = 401060300;
//时间到了或者死了之后回去的地图
var backMap = 401060000;

function init() {
	em.setProperty("leader", "true");
	em.setProperty("state", "0");
}

function setup(eim, leaderid) {
	em.setProperty("leader", "true");
	em.setProperty("state", "1");
	var eim = em.newInstance("BossMagnus_EASY");
	var map = eim.setInstanceMap(startMap);
	map.resetFully();
	map.killAllMonsters(false);
	var mob = em.getMonster(8880010);
	mob.getStats().setChange(true);
	mob.changeLevel(110);
	mob.getChangedStats().setOHp(1000000000);
	mob.setHp(1000000000);
	eim.registerMonster(mob);
	eim.setProperty("HPstate", "1");
	map.spawnMonsterOnGroundBelow(mob, new java.awt.Point(1860, -1450));
	eim.startEventTimer(3600000); // 1 hr
	eim.schedule("summonFall", 5000);
	eim.schedule("checkChrHP", 2000);
	eim.schedule("checkChrBUFF", 1000);
	return eim;
}

function playerEntry(eim, player) {
	var map = eim.getMapInstance(0);
	player.setReviveCount(player.isAdmin() ? GMreviveCount : reviveCount);
	player.changeMap(map, map.getPortal(0));
}

function playerRevive(eim, player) {
	if (player.getReviveCount() > 0) {
		var map = player.getMap();
		player.eventRevive(map, map.getPortal(0));
		return true;
	}
	player.addHP(50);
	var map = eim.getMapFactoryMap(backMap);
	player.changeMap(map, map.getPortal(0));
	return true;
}

function scheduledTimeout(eim) {
	eim.disposeIfPlayerBelow(100, backMap);
	em.setProperty("state", "0");
	em.setProperty("leader", "true");
}

function changedMap(eim, player, mapid) {
	switch (mapid) {
	case startMap:
		break;
	default:
		eim.unregisterPlayer(player);
		if (eim.disposeIfPlayerBelow(0, 0)) {
			player.restReviveCount();
			em.setProperty("state", "0");
			em.setProperty("leader", "true");
		}

		break;
	}
}

function playerDisconnected(eim, player) {
	return 0;
}

function monsterValue(eim, mobId) {
	return 1;
}

function playerExit(eim, player) {
	eim.unregisterPlayer(player);

	if (eim.disposeIfPlayerBelow(0, 0)) {
		em.setProperty("state", "0");
		em.setProperty("leader", "true");
	}
}

function end(eim) {
	if (eim.disposeIfPlayerBelow(100, backMap)) {
		em.setProperty("state", "0");
		em.setProperty("leader", "true");
	}
}

function clearPQ(eim) {
	end(eim);
}

function allMonstersDead(eim) {
	eim.setProperty("HPstate", "-1");
}

function leftParty(eim, player) {}
function disbandParty(eim) {}
function playerDead(eim, player) {}
function cancelSchedule() {}

function summonFall(eim) {
	var state = parseInt(eim.getProperty("HPstate"));
	var time = 4500;
	if (state > 0) {
		var map = eim.getMapInstance(0);
		map.obtacleFall(2 * state + 1, 1, 8);
		eim.schedule("summonFall", time);
	}
}

function checkChrHP(eim) {
	var state = parseInt(eim.getProperty("HPstate"));
	if (state > 0) {
		var map = eim.getMapInstance(0);
		var mob = map.getMonsterById(8880010);
		if (mob != null) {
			for (i = 0; i < eim.getPlayers().size(); i++) {
				mob.checkMobZone(eim.getPlayers().get(i), true);
			}
			eim.schedule("checkChrHP", 2000);
		}
	}
}

function checkChrBUFF(eim) {
	var state = parseInt(eim.getProperty("HPstate"));
	if (state > 0) {
		var map = eim.getMapInstance(0);
		var mob = map.getMonsterById(8880010);
		if (mob != null) {
			for (i = 0; i < eim.getPlayers().size(); i++) {
				mob.checkMobZone(eim.getPlayers().get(i), false);
			}
			eim.schedule("checkChrBUFF", 1000);
		}
	}
}
function monsterDrop(eim, player, mob) {}
function pickUpItem(eim, player, itemID) {}
